<?php

namespace common\helpers;

use Yii;

/**
 * 自定义实用工具类
 *
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class Curl {

    /**
     * 判断远程文件是否存在
     *
     * @param int $url 远程文件地址
     * @return boolean
     */
    public static function checkRemoteFileExists($url) {
        $curl = curl_init($url);
        // 不取回数据
        curl_setopt($curl, CURLOPT_NOBODY, true);
        // 发送请求
        $result = curl_exec($curl);
        $found = false;
        // 如果请求没有发送失败
        if ($result !== false) {
            // 再检查http响应码是否为200
            $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if ($statusCode == 200) {
                $found = true;
            }
        }
        curl_close($curl);
        return $found;
    }

    /**
     * GET 请求
     * @param string $url
     * @param string $returnType content|status|code
     */
    public static function get($url, $returnType = 'content') {
        $ch = curl_init();
        $scheme = parse_url($url, PHP_URL_SCHEME);
        if ($scheme == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $content = curl_exec($ch);
        $status = curl_getinfo($ch);

        if ($returnType == 'content') {
            if (intval($status["http_code"]) == 200) {
                return $content;
            } else {
                return false;
            }
        } elseif ($returnType == 'status') {
            return $status;
        } elseif ($returnType == 'code') {
            return $status["http_code"];
        } else {
            return compact('content', 'status');
        }
    }

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @return string content
     */
    public static function post($url, $param) {
        $ch = curl_init();
        $scheme = parse_url($url, PHP_URL_SCHEME);
        if ($scheme == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        $strPOST = false;
        if (is_array($param)) {
            $strPOST = http_build_query($param);
        } elseif (is_string($param)) {
            $strPOST = $param;
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, true);
        if ($strPOST !== false) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $strPOST);
        }
        $sContent = curl_exec($ch);
        $aStatus = curl_getinfo($ch);
        curl_close($ch);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    /**
     * 远程图片是否存在
     * @param string $url 远程文件url地址
     * @return boolean
     */
    public static function remoteFileExists($url) {
        $file_headers = get_headers($url);
        if ($file_headers[0] == 'HTTP/1.1 404 Not Found') {
            return false;
        }
        return true;
    }

}
